//
//  PaymentViewController.m
//  CPICApp
//
//  Created by lizhiwei on 13-3-20.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "PaymentViewController.h"
#import "Config.h"
#import "UIImageView+WebCache.h"
#import "RCAlertViewUtil.h"
#import "Appdelegate.h"

@interface PaymentViewController ()

@end

@implementation PaymentViewController
@synthesize TablePayInfo;
@synthesize ArrtpolicypaymentList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.title = @"保单缴费信息";
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    NSString *bgimage= [accountDefaults objectForKey:@"UserDefaultBgKindKey"];
    
    if ([bgimage isEqualToString:@"蓝色主题风格"]) 
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88@2x.png");
        ADD_LEFT_BUTTONITEM(@"back.png");
        ADD_TITLE_NAMEITEM(@"保单缴费信息");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    else if([bgimage isEqualToString:@"棕色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88_01@2x.png");
        ADD_LEFT_BUTTONITEM(@"back_01.png");
        ADD_TITLE_NAMEITEM(@"保单缴费信息");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773_01@2x"]];
    }
    else if([bgimage isEqualToString:@"重大节日皮肤"])
    {
        NSData *dataImg_02=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_02];
        NSLog(@"-------找到%@",dataImg_02);
        NSData *dataImg_03=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_03];
        NSData *dataImg_04=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_04];
        
        NSURL *urlSkinImg_02=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_02]];
        NSURL *urlSkinImg_03=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_03]];
        NSURL *urlSkinImg_04=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_04]];
        NSLog(@"-------找到333%@",urlSkinImg_02);
        
        UIImageView * LogoImage = [[UIImageView alloc]init];
        [LogoImage setImageWithURL:urlSkinImg_03 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(LogoImage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_03];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        LogoImage.image = [UIImage imageWithData:dataImg_03];
        LogoImage.frame = CGRectMake(0, 0, LogoImage.image.size.width/2, LogoImage.image.size.height/2);
        [self.view addSubview:LogoImage];
        
        //--------------------------
        UIButton * CallOutBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        CallOutBtn1.frame = CGRectMake(10, 10, 109/2, 57/2);
        UIImageView * Back = [[UIImageView alloc]init];
        [Back setImageWithURL:urlSkinImg_02 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(Back.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_02];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [Back setImage:[UIImage imageWithData:dataImg_02]];
        [CallOutBtn1 setImage:Back.image forState:UIControlStateNormal];
        [CallOutBtn1 addTarget:self action:@selector(ClickCallOutBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:CallOutBtn1];
        //--------------------------
        
        ADD_TITLE_NAMEITEM(@"保单缴费信息");
        
        
        UIImageView *bgimage = [[UIImageView alloc]initWithFrame:CGRectMake(0,44, self.view.frame.size.width, self.view.frame.size.height-44)];
        [bgimage setImageWithURL:urlSkinImg_04 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(bgimage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_04];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [bgimage setImage:[UIImage imageWithData:dataImg_04]];
        [self.view addSubview:bgimage];
        [bgimage release];
    }

    
    //初始化列表
    TablePayInfo = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-44) style:UITableViewStyleGrouped];
    TablePayInfo.dataSource = self;
    TablePayInfo.delegate = self;
    TablePayInfo.scrollEnabled = NO;
    [self.view addSubview:TablePayInfo];
    
    ArrayPayInfo = [[NSMutableArray alloc]initWithObjects:@"保单号",@"支付对象",@"支付方式",@"支付方银行帐号",@"支付金额",@"支付日期",nil];
    
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    myDelegate.bInsurancePolicySearch=TRUE;
}

#pragma UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ArrtpolicypaymentList count];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier=@"Identifier";
    UITableViewCell *cell=(UITableViewCell *)[TablePayInfo dequeueReusableCellWithIdentifier:Identifier];
    if (cell==nil)
    {
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中后颜色没变化
        
        DictpolicypaymentList = [self.ArrtpolicypaymentList objectAtIndex:indexPath.row];
        
        UILabel *lblpolicyno=[[UILabel alloc]initWithFrame:CGRectMake(30, 14.5, 210, 21)];
        lblpolicyno.backgroundColor=[UIColor clearColor];
        lblpolicyno.text=[ArrayPayInfo objectAtIndex:0];
        lblpolicyno.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpolicyno];
        [lblpolicyno release];
        
        UILabel *lblpayobject=[[UILabel alloc]initWithFrame:CGRectMake(30, 44.5, 210, 21)];
        lblpayobject.backgroundColor=[UIColor clearColor];
        lblpayobject.text=[ArrayPayInfo objectAtIndex:1];
        lblpayobject.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpayobject];
        [lblpayobject release];
        
        UILabel *lblpaytypename=[[UILabel alloc]initWithFrame:CGRectMake(30, 74.5, 210, 21)];
        lblpaytypename.backgroundColor=[UIColor clearColor];
        lblpaytypename.text=[ArrayPayInfo objectAtIndex:2];
        lblpaytypename.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpaytypename];
        [lblpaytypename release];
        
        UILabel *lblpaybankaccout=[[UILabel alloc]initWithFrame:CGRectMake(30, 104.5, 210, 21)];
        lblpaybankaccout.backgroundColor=[UIColor clearColor];
        lblpaybankaccout.text=[ArrayPayInfo objectAtIndex:3];
        lblpaybankaccout.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpaybankaccout];
        [lblpaybankaccout release];
        
        UILabel *lblpayamount=[[UILabel alloc]initWithFrame:CGRectMake(30, 134.5, 210, 21)];
        lblpayamount.backgroundColor=[UIColor clearColor];
        lblpayamount.text=[ArrayPayInfo objectAtIndex:4];
        lblpayamount.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpayamount];
        [lblpayamount release];
        
        UILabel *lblpaydate=[[UILabel alloc]initWithFrame:CGRectMake(30, 164.5, 210, 21)];
        lblpaydate.backgroundColor=[UIColor clearColor];
        lblpaydate.text=[ArrayPayInfo objectAtIndex:5];
        lblpaydate.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpaydate];
        [lblpaydate release];
        
        UILabel *lblpolicynoValue=[[UILabel alloc]initWithFrame:CGRectMake(130, 14.5, 210, 21)];
        lblpolicynoValue.backgroundColor=[UIColor clearColor];
        lblpolicynoValue.text=[[NSString alloc] initWithFormat:@"%@",[DictpolicypaymentList objectForKey:@"policyno"]];
        lblpolicynoValue.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpolicynoValue];
        [lblpolicynoValue release];
        
        UILabel *lblpayobjectValue=[[UILabel alloc]initWithFrame:CGRectMake(130, 44.5, 210, 21)];
        lblpayobjectValue.backgroundColor=[UIColor clearColor];
        lblpayobjectValue.text=[[NSString alloc] initWithFormat:@"%@",[DictpolicypaymentList objectForKey:@"payobject"]];
        lblpayobjectValue.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpayobjectValue];
        [lblpayobjectValue release];
        
        UILabel *lblpaytypenamevalue=[[UILabel alloc]initWithFrame:CGRectMake(130, 74.5, 210, 21)];
        lblpaytypenamevalue.backgroundColor=[UIColor clearColor];
        lblpaytypenamevalue.text=[[NSString alloc] initWithFormat:@"%@",[DictpolicypaymentList objectForKey:@"paytypename"]];
        lblpaytypenamevalue.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpaytypenamevalue];
        [lblpaytypenamevalue release];
        
        UILabel *lblpaybankaccoutvalue=[[UILabel alloc]initWithFrame:CGRectMake(140, 104.5, 210, 21)];
        lblpaybankaccoutvalue.backgroundColor=[UIColor clearColor];
        lblpaybankaccoutvalue.text=[[NSString alloc] initWithFormat:@"%@",[DictpolicypaymentList objectForKey:@"paybankaccout"]];
        lblpaybankaccoutvalue.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpaybankaccoutvalue];
        [lblpaybankaccoutvalue release];
        
        UILabel *lblpayamountvalue=[[UILabel alloc]initWithFrame:CGRectMake(130, 134.5, 210, 21)];
        lblpayamountvalue.backgroundColor=[UIColor clearColor];
        lblpayamountvalue.text=[[NSString alloc] initWithFormat:@"%@",[DictpolicypaymentList objectForKey:@"payamount"]];
        lblpayamountvalue.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpayamountvalue];
        [lblpayamountvalue release];
        
        UILabel *lblpaydatevalue=[[UILabel alloc]initWithFrame:CGRectMake(130, 164.5, 210, 21)];
        lblpaydatevalue.backgroundColor=[UIColor clearColor];
        lblpaydatevalue.text=[[NSString alloc] initWithFormat:@"%@",[DictpolicypaymentList objectForKey:@"paydate"]];
        lblpaydatevalue.font = [UIFont systemFontOfSize:15.0];
        [cell addSubview:lblpaydatevalue];
        [lblpaydatevalue release];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(void) ClickCallOutBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(id)initView:(NSMutableArray *)policypaymentList
{
    self.ArrtpolicypaymentList=policypaymentList;
    
    self = [super init];
    
    return self;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [TablePayInfo release];
    [ArrtpolicypaymentList release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
